/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.googlecode.ivybeans.module.customizer;

import com.googlecode.ivybeans.module.FileHelper;
import com.googlecode.ivybeans.module.IvyConfigurationHelper;
import com.googlecode.ivybeans.module.IvyLibraryController;
import com.googlecode.ivybeans.module.classpath.ClassPathScope;
import com.googlecode.ivybeans.module.resources.ResourcesAccess;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import org.netbeans.api.project.Project;
import org.openide.filesystems.FileUtil;
import org.openide.util.NbBundle;

public class IvyLibraryUi {

    public static EditMediator registerEditMediatorProject(Project project, JCheckBox ivyEnabled, JButton ivyFileBtn, JTextField ivyFileTxt, JButton settingsFileBtn, JTextField settingsFileTxt, JCheckBox autoResolveCheck, JTable confTable) {
        EditMediator em = new EditMediator(project, ivyEnabled, ivyFileBtn, ivyFileTxt, settingsFileBtn, settingsFileTxt, autoResolveCheck, confTable);

        IvyLibraryController ivyLibraryController = project.getLookup().lookup(IvyLibraryController.class);
        em.setEnabledAllComponents(ivyLibraryController.isIvyEnabled());
        ivyEnabled.addActionListener(em);

        File descriptorFile = ivyLibraryController.getDescriptorFile();
        if (descriptorFile != null) {
            String descriptorText = FileHelper.getInstance().getPath(
                    project.getProjectDirectory(),
                    descriptorFile);
            ivyFileTxt.setText(descriptorText);
        }
        ivyFileBtn.addActionListener(em);

        File settingsFile = ivyLibraryController.getSettingsFile();
        URL settingsURL = ivyLibraryController.getSettingsURL();
        if (settingsFile != null) {
            String settingsText = FileHelper.getInstance().getPath(
                    project.getProjectDirectory(),
                    settingsFile);
            settingsFileTxt.setText(settingsText);
        } else if (settingsURL != null) {
            settingsFileTxt.setText(settingsURL.toString());
        }
        settingsFileBtn.addActionListener(em);

        autoResolveCheck.setSelected(ivyLibraryController.isAutoResolve());
        confTable.setModel(new ConfigurationTableModel(ivyLibraryController.getConfigurationMapping()));

        return em;
    }

    public final static class EditMediator implements ActionListener {

        private final Project project;
        private JCheckBox ivyEnabledCheck;
        private JButton ivyFileBtn;
        private JTextField ivyFileTxt;
        private JButton settingsFileBtn;
        private JTextField settingsFileTxt;
        private JTable confTable;
        private JCheckBox autoResolveCheck;

        private EditMediator(Project project, JCheckBox ivyEnabledCheck, JButton ivyFileBtn, JTextField ivyFileTxt, JButton settingsFileBtn, JTextField settingsFileTxt, JCheckBox autoResolveCheck, JTable confTable) {
            this.project = project;
            this.ivyEnabledCheck = ivyEnabledCheck;
            this.ivyFileBtn = ivyFileBtn;
            this.ivyFileTxt = ivyFileTxt;
            this.settingsFileBtn = settingsFileBtn;
            this.settingsFileTxt = settingsFileTxt;
            this.autoResolveCheck = autoResolveCheck;
            this.confTable = confTable;
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(ivyEnabledCheck)) {
                setEnabledAllComponents(ivyEnabledCheck.isSelected());
            }

            if (e.getSource().equals(ivyFileBtn)) {
                createFileChooser(
                        NbBundle.getMessage(IvyLibraryUi.class, "ivyFile.FileChooser.DialogTitle"),
                        ivyFileBtn,
                        ivyFileTxt);
            }
            if (e.getSource().equals(settingsFileBtn)) {
                createFileChooser(
                        NbBundle.getMessage(IvyLibraryUi.class, "settingsFile.FileChooser.DialogTitle"),
                        settingsFileBtn,
                        settingsFileTxt);
            }
        }

        private void createFileChooser(String dialogTitle, JButton sourceButton, JTextField textField) {
            JFileChooser chooser = new JFileChooser();
            FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            chooser.setMultiSelectionEnabled(false);
            chooser.setDialogTitle(dialogTitle); // NOI18N

            File curDir = FileUtil.toFile(project.getProjectDirectory());
            chooser.setCurrentDirectory(curDir);

            int option = chooser.showOpenDialog(SwingUtilities.getWindowAncestor(sourceButton)); // Sow the chooser

            if (option == JFileChooser.APPROVE_OPTION) {
                String path = FileHelper.getInstance().getPath(
                        project.getProjectDirectory(),
                        chooser.getSelectedFile());
                textField.setText(path);
            }
        }

        public void setEnabledAllComponents(boolean b) {
            ivyEnabledCheck.setSelected(b);
            ivyFileTxt.setEnabled(b);
            ivyFileBtn.setEnabled(b);
            settingsFileTxt.setEnabled(b);
            settingsFileBtn.setEnabled(b);
            confTable.setEnabled(b);
            autoResolveCheck.setEnabled(b);
            autoResolveCheck.setSelected(b);
        }

        public Project getProject() {
            return project;
        }

        public JCheckBox getIvyEnabledCheck() {
            return ivyEnabledCheck;
        }

        public JButton getIvyFileBtn() {
            return ivyFileBtn;
        }

        public JTextField getIvyFileTxt() {
            return ivyFileTxt;
        }

        public JButton getSettingsFileBtn() {
            return settingsFileBtn;
        }

        public JTextField getSettingsFileTxt() {
            return settingsFileTxt;
        }

        public JCheckBox getAutoResolveCheck() {
            return autoResolveCheck;
        }

        public JTable getConfTable() {
            return confTable;
        }
    }

    public static class ConfigurationTableModel extends DefaultTableModel {

        public ConfigurationTableModel(IvyLibraryController.ConfigurationMapping configurationMapping) {
            super(new String[]{
                        NbBundle.getMessage(IvyLibraryUi.class, "confTable.columnModel.title1"),
                        NbBundle.getMessage(IvyLibraryUi.class, "confTable.columnModel.title2")
                    }, 0);
            
            for (ClassPathScope scope : ClassPathScope.values()) {
                String scopeDisplayName = NbBundle.getMessage(ResourcesAccess.class, "CTL_Scope_"+scope.name());
                String configuration = IvyConfigurationHelper.getInstance().untokenize(configurationMapping.getConfiguration(scope));
                insertRow(scope.ordinal(), new String[]{scopeDisplayName, configuration});
            }
            
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return column > 0;
        }
        
        
    }
}
